package cn.ha.toy.entity;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class UserValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		User user = (User) target;
		if( !StringUtils.isEmpty(user.getName()) 
				&& user.getName().length() >= 8 ){
			errors.rejectValue("name", "valid.nameLen", 
					new Object[]{"minLength",8},"用户名不能少于{1}位");
		}
	}

}
